package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.api.db.QuestionApi;
import com.tanhua.dubbo.api.mapper.QuestionMapper;
import com.tanhua.model.db.Question;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class QuestionApiImpl implements QuestionApi {

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 通过用户id查询陌生人问题
     *
     * @param userId
     * @return
     */
    @Override
    public Question findByUserId(Long userId) {
        LambdaQueryWrapper<Question> lqw = new LambdaQueryWrapper();
        lqw.eq(Question::getUserId, userId);
        return questionMapper.selectOne(lqw);
    }

    /**
     * 保存陌生人问题
     *
     * @param question
     */
    @Override
    public void save(Question question) {
        //1. 通过用户id查询是否存在
        Question questionInDB = findByUserId(question.getUserId());
        //2. 如果存在则更新
        if(null != questionInDB){
            question.setId(questionInDB.getId());
            questionMapper.updateById(question);
        }else {
            //3. 不存在则添加记录
            questionMapper.insert(question);
        }
    }
}
